// pages/newEvent/newEvent.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModalStatus: false,
    date: '选择开始时间',
    color:"#CBCBCB",
    color2:'#CBCBCB',
    showDay:false,
    showHidden:false,
    focus:false,
    color3: '#CBCBCB',
    color4: '#CBCBCB',
    addImg:'',
    addHidden:true,
    bag:0,
    typelist:[],
    typename:'',
    start_time:'',
    dur_time:'',
    dur_temp:'',
    type_id:0,
    eventHidden:false,
    textHidden:true,
    descript:'',
    i:0,
    photo:''
  },
  //删除img
  deleImg: function (e) {
    console.log(e)
    var that = this
    var index = e.target.dataset.index;
    var images = that.data.addImg;
    images.splice(index, 1);
    that.setData({
      addImg: images
    });
  },
  bindViewTap: function () {
    var that = this;
    wx.chooseImage({
      // 设置最多可以选择的图片张数，默认9,如果我们设置了多张,那么接收时//就不在是单个变量了,
      count: 4,
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        // 获取成功,将获取到的地址赋值给临时变量
        var tempFilePaths = res.tempFilePaths;
        that.setData({
          //将临时变量赋值给已经在data中定义好的变量
          addImg: tempFilePaths,
          addHidden:false
        })
      },
      fail: function (res) {
        // fail
      },
      complete: function (res) {
        // complete
      }
    })
  },
  navig:function () {
    // wx.navigateTo({
    //   url: '../description/description'
    // })
    this.setData({
      color4:'',
      eventHidden:true,
      textHidden:false
    })
  },
  send:function () {
    this.setData({
      color4: '',
      eventHidden: false,
      textHidden: true
    })
  },
  selet_confirm:function () {
    var dur = this.data.dur_temp;
    this.setData({
      focus:false,
      showHidden:false,
      showDay:false,
      color3:'',
      dur_time:dur

    })
  },
  //持续天数
  selectDay: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showHidden: true,
      showDay:true,
      focus:true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  leixing:function(e) {
    console.log(e.currentTarget.dataset.index)
    this.setData({
      color2:'',
      showModalStatus:false,
      showHidden:false,
      bag: e.currentTarget.dataset.index,
      typename: e.currentTarget.dataset.name,
      type_id: e.currentTarget.dataset.id,
    })
  },
  bindDateChange: function (e) {
    this.setData({
      start_time: e.detail.value,
      color:""
    })
  },
  //事件类型
  selectGenre: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true,
      showHidden:true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //点击隐藏
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false,
        showDay:false,
        showHidden:false,
        focus:false
      })
    }.bind(this), 200)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Event/getType',
      method: "post",
      data: {
  
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.status == 1) {
          that.setData({
            typelist: res.data.typelist
          });
        } else {
          wx.showToast({
            title: res.data.err,
          })
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 30000
        });
      },
    })
  },

  getDur:function (e) {
    var dur_temp = e.detail.value;
    this.setData({
      dur_temp:dur_temp
    });
  },
  getDesc: function (e) {
    var descript = e.detail.value;
    this.setData({
      descript: descript
    });
  },

  baocun:function (e){
    var that = this;
    var addImg = that.data.addImg;
    if (addImg != '') {
      that.upload();
    } else {
      that.save();
    }
  },

  upload: function (e) {
    var that = this;
    var addImg = that.data.addImg;
    var i = that.data.i;
    var length = addImg.length;
    wx.uploadFile({
      url: app.d.ceshiUrl + '/Api/Event/uploadimg',
      filePath: addImg[i],
      name: 'data',
      success: function (res) {
        console.log('uploadImage success, res is:', res);
        that.setData({
          photo: that.data.photo.concat(res.data) + ',',
          i: that.data.i + 1,
        })
        if (that.data.i < length) {
          that.upload();
        } else {
          that.save();
        }
      },

      fail: function ({ errMsg }) {
        console.log('uploadImage fail, errMsg is', errMsg)
        return false;
      }
    })
  },
  save: function (e) {
    var that = this;
    var addImg = that.data.addImg;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Event/add',
      method: "post",
      data: {
        uid: app.d.userId,
        photo: that.data.photo,
        type_id: that.data.type_id,
        start_time: that.data.start_time,
        dur_time: that.data.dur_time,
        descript: that.data.descript,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.status == 1) {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
          setTimeout(function () {
            wx.navigateBack({

            })
          }, 2000)

        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          })
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 30000
        });
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})